package com.mp.codegenerator;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.ArrayList;
import java.util.List;

/**
 *  mybatis plus的代码生成器
 * @author: notejava
 * @date: 2020年7月10号
 */
public class CodeGenerator {

    public static void main(String[] args) {
        //1、创建代码生成器对象
        AutoGenerator mpg = new AutoGenerator();

        //2、全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        gc.setOutputDir(projectPath+"/src/main/java");
        gc.setAuthor("notejava");
        gc.setOpen(false);
        gc.setFileOverride(false);//是否覆盖
        gc.setServiceName("%sService");//去掉Service的I 前缀
        gc.setIdType(IdType.ID_WORKER);//雪花算法生成id
        gc.setDateType(DateType.ONLY_DATE);
        gc.setSwagger2(true);//设置swagger
        mpg.setGlobalConfig(gc);

        //3、数据源配置
        DataSourceConfig dc = new DataSourceConfig();
        dc.setUrl("jdbc:mysql://localhost:3306/mp?useSSL=true&characterEncoder=utf-8");
        dc.setDriverName("com.mysql.jdbc.Driver");
        dc.setUsername("root");
        dc.setPassword("root");
        dc.setDbType(DbType.MYSQL);
        mpg.setDataSource(dc);

        //4、包设置
        PackageConfig pc = new PackageConfig();
        pc.setModuleName("goods");
        pc.setParent("com.mp");
        pc.setController("controller");
        pc.setService("service");
        pc.setMapper("mapper");
        pc.setEntity("bean");
        mpg.setPackageInfo(pc);

        //5、策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setInclude("std_value");//映射的表名
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(true);
        strategy.setLogicDeleteFieldName("del_flag"); //逻辑删除
        //自动填充
        TableFill createTime = new TableFill("create_time", FieldFill.INSERT);
        TableFill updateTime = new TableFill("update_time", FieldFill.INSERT_UPDATE);
        List<TableFill> tableFills = new ArrayList<>();
        tableFills.add(createTime);
        tableFills.add(updateTime);
        strategy.setTableFillList(tableFills);
        //乐观锁
        strategy.setVersionFieldName("version");
        strategy.setRestControllerStyle(true);
        strategy.setControllerMappingHyphenStyle(true);//url中允许使用下划线 如locahost:8080/user_id_2
        mpg.setStrategy(strategy);

        //执行
        mpg.execute();



    }


}
